var mainFrame = $('body');
mainFrame.query = mainFrame.find('#query');
mainFrame.searchBtn = mainFrame.find('#search');
mainFrame.askQuestionBtn = mainFrame.find('#post-problem');
mainFrame.viewRatingBtn = mainFrame.find('#view-rating');

var questionEditor = $('#editor');
questionEditor.saveBtn = questionEditor.find('input[name=save]');
questionEditor.cancelBtn = questionEditor.find('input[name=cancel]');
questionEditor.tid = null;
questionEditor.textarea = questionEditor.find('textarea');
questionEditor.cleanup = cleanUpTextArea;
questionEditor.edit = edit;
questionEditor.isEmpty = isTextAreaEmpty;

var answerEditor = $('#comments-bar');
answerEditor.saveBtn = answerEditor.find('input[name=save]');
answerEditor.cancelBtn = answerEditor.find('input[name=cancel]');
answerEditor.tid = null;
answerEditor.textarea = answerEditor.find('textarea');
answerEditor.cleanup = cleanUpTextArea;
answerEditor.edit = edit;
answerEditor.isEmpty = isTextAreaEmpty;

var ratingPnl = $('#rating');

var questionToolbar = $('#toolbar');
questionToolbar.voteUpBtn = questionToolbar.find('.vote-btn-up');
questionToolbar.voteDownBtn = questionToolbar.find('.vote-btn-down');
questionToolbar.editBtn = questionToolbar.find('.vote-btn-edit');
questionToolbar.openBtn = questionToolbar.find('.vote-btn-open');
questionToolbar.attach = attach;
questionToolbar.detach = detach;

var answerToolbar = $('#toolbar2');
answerToolbar.voteUpBtn = answerToolbar.find('.vote-btn-up');
answerToolbar.voteDownBtn = answerToolbar.find('.vote-btn-down');
answerToolbar.editBtn = answerToolbar.find('.vote-btn-edit');
answerToolbar.attach = attach;
answerToolbar.detach = detach;

var questionsList = $('#content');
questionsList.getSelected = getSelected;

var commentsList = $('#comments-list');
commentsList.getAnswer = getAnswer;

var dock = $('#dock');

var qtabHeight = 114;

$('textarea').autosize();
$('textarea').val('');
mainFrame.query.limit(60);
questionEditor.textarea.limit(1400);
answerEditor.textarea.limit(1100);


function edit(holder) {
    this.tid = holder.getId();
    var textarea = this.textarea;
    var content = holder.getContent();
    textarea.val(content);
    textarea.focus();
    textarea.resize();
}

function cleanUpTextArea() {
    this.tid = null;
    var textarea = this.textarea;
    textarea.val('');
    textarea.focus();
    textarea.resize();
}

function isTextAreaEmpty() {
    var txtarea = this.textarea;
    var value = txtarea.val();
    value = jQuery.trim(value);
    return value == 0;
}

function getSelected() {
    return questionsList.find('.selected')
}

function attach(item) {
    var footer = $(item).find('div[name="footer"]');
    var aperms = $(item).find('input[name="aperms"]').val();

    if (aperms == '11') {
        this.editBtn.show();
    }
    else if (aperms == '01') {
        this.voteUpBtn.show();
        this.voteDownBtn.show();
    }
    if (this.openBtn != null) {
        this.openBtn.show();
    }
    footer.append(this);
}

function detach() {
    this.find('div').hide();
    dock.append(this);
}

function getAnswer(cid) {
    this.find('#answer_' + cid)
}

jQuery.fn.getHolder = function () {
    return this.parentsUntil('.holder').parent()
};

jQuery.fn.getId = function () {
    return this.find('input[name="cid"]').val();
};

jQuery.fn.getContent = function () {
    var text = this.find('.content:first').html().trim();
    text = text.replace(/<span class="qhlight">/g, '')
               .replace(/<\/span>/g, '');
    text = text.replace(/<br>/g, '\n')
               .replace(/&nbsp;/g, ' ')
               .replace(/&gt;/g, '>')
               .replace(/&lt;/g, '<');
    return text
};

jQuery.fn.setContent = function (data) {
    data = data.replace(/>/g, '&gt;')
               .replace(/</g, '&lt;')
               .replace(/\n/g, '<br>')
               .replace(/ /g, '&nbsp;');
    this.find('.content:first').html(data)
};